import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

  const routes = [

  {
    path: '/',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '@/views/Login.vue')
  },
  {
    path: '/none',
    name: 'none',
    component: () => import(/* webpackChunkName: "login" */ '@/views/none.vue')
  },
  {
    path: '/retrievePassword',  //找回密码
    name: 'retrievePassword',
    component: () => import(/* webpackChunkName: "login" */ '@/views/retrievePassword.vue')
  },
  {
    path: '/user',
    name: 'user',
    redirect:"/user/user",
    component: () => import(/* webpackChunkName: "user" */ '@/views/user/index.vue'),
    children:[
      {
        path:"/user/userheader",      //用户头像
        name:"userheader",
        component: () => import(/* webpackChunkName: "user" */ '@/views/user/userHeader.vue'),
      },
      {
        path:"/user/user",    //user主页
        name:"user",
        component: () => import(/* webpackChunkName: "user" */ '@/views/user/user.vue'),
      },
      {
        path:"/user/upgard",    //成绩上传页
        name:"upgard",
        component: () => import(/* webpackChunkName: "user" */ '@/views/user/upgard.vue'),
      },
      {
        path:"/user/setting",    //用户设置页面
        name:"setting",
        component: () => import(/* webpackChunkName: "user" */ '@/views/user/setting.vue'),
      },
      {
        path:"/user/newTest",    //用户设置页面
        name:"newTest",
        component: () => import(/* webpackChunkName: "user" */ '@/views/user/newTest.vue'),
      },
    ]
  },
  {
    path: '/register',
    name: 'register',
    component: () => import(/* webpackChunkName: "login" */ '@/views/register.vue')
  },

  {
    path: '/uploadProject',    //上传成绩核心页面
    name: 'uploadProject',
    component: () => import(/* webpackChunkName: "home" */ '@/views/home/uploadProject.vue')
  },
  {
    path: '/uploadgard/:id',    //修改成绩核心页面
    name: 'uploadgard',
    component: () => import(/* webpackChunkName: "home" */ '@/views/user/uploadgard.vue')
  },
  {
    path: '/home',
    name: 'home',
    redirect:"/home/index",
    component: () => import(/* webpackChunkName: "home" */ '@/views/home.vue'),
    children:[
      {
        path:"/home/index",
        name:"homeIndex",
        component: () => import(/* webpackChunkName: "poject" */ '@/views/home/index.vue')
      },
      {
        path:"/project/grade/:id",
        name:"grade",
        component: () => import(/* webpackChunkName: "poject" */ '@/views/project/grade.vue')
      },

      {
        path:"/project/practice/:id",
        name:"grade",
        component: () => import(/* webpackChunkName: "poject" */ '@/views/project/practice.vue')
      },

      {
        path:"/study/english/:id",
        name:"english",
        component: () => import(/* webpackChunkName: "poject" */ '@/views/study/english.vue')
      },
   
    ]
  },
  {
    path:"*",
    name:"404",
    component: () => import(/* webpackChunkName: "login" */ '@/views/404.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
      //to 将要跳转到那个路由
      //from 来至哪个路由
      //next 表示放行，有两种方式 next()  next("/home")
  
      if (to.path === "/" || to.path==="/register" ||to.path==="/retrievePassword") {
          return next()
      } else {
          //获取在登陆时返回数据里面保存在内存中的token值
          const tokenStr = window.sessionStorage.getItem("token")
  // console.log(tokenStr)
          if (!tokenStr) {
              return next("/")
          } else {
              next()
          }
      }
  })

export default router
